package client.gui;

import javax.swing.*;

import core.domein.TextReceivedListener;

import client.domein.GameChatController;
import client.domein.dammen.*;
import java.net.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class DammenFrame extends javax.swing.JFrame 
{
	private static final long serialVersionUID = 1L;
	private DammenPanel dammenPanel;
	private DammenGame game;
	private JTextArea txtErrors;
	private JButton btnSend;
	private JTextArea txtReceived;
	private JSeparator jsep2;
	private JSeparator jsep1;
	private JLabel lblFouten;
	private JTextField txtMessage;
	private JScrollPane sRec, sErr;
	private SimpleDateFormat sdf;
	private GameChatController gc;
	private String mijnNick, zijnNick;
	private LobbyFrame lobby;

	public DammenFrame(int verzendPoort, int ontvangPoort, String mijnNick, String zijnNick, 
			InetAddress address, boolean mijnBeurt, LobbyFrame frame) 
	{
		super();
		game = new DammenGame(new Point(8,8), 12, address, verzendPoort, ontvangPoort, mijnBeurt);
		this.mijnNick = mijnNick;
		this.zijnNick = zijnNick;
		gc = game.getGcController();
		gc.addTextReceivedListener(new TextReceivedListener() {
			public void textReceived(String text)
			{
				messageReceived(text);
			}
		});
		initGUI();
		centerScreen();
		sdf = new SimpleDateFormat("[HH:mm:ss] ");
		this.lobby = frame;
		
		this.addWindowListener(new WindowAdapter()
		{
		    public void windowClosing(WindowEvent w)
		    {
		    	game.closeGame();
		    	lobby.setDammenEinde();
		    }
		});
		
	}
	
	private void centerScreen()
	{	
		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		int x = ((int)d.getWidth() - this.getSize().width) / 2;
		int y = ((int)d.getHeight() - this.getSize().height) / 2;
		this.setLocation(x,y);
	}
	
	private void initGUI() {
		try {
			this.setLayout(null);
			{
				dammenPanel = new DammenPanel(this, game);
				this.add(dammenPanel);
				dammenPanel.setBounds(0,0,601,600);
			}
			{
				txtMessage = new JTextField();
				getContentPane().add(txtMessage);
				txtMessage.setBounds(609, 280, 196, 21);
				txtMessage.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						messageActionPerformed(evt);
					}
				});
			}
			{
				txtReceived = new JTextArea();
				sRec = new JScrollPane(txtReceived);
				getContentPane().add(sRec);
				sRec.setBounds(609, 7, 273, 259);
				txtReceived.setEditable(false);
			}
			{
				btnSend = new JButton();
				getContentPane().add(btnSend);
				btnSend.setText("Send");
				btnSend.setBounds(812, 280, 70, 21);
				btnSend.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						messageActionPerformed(evt);
					}
				});
			}
			{
				txtErrors = new JTextArea();
				sErr = new JScrollPane(txtErrors);
				getContentPane().add(sErr);
				sErr.setBounds(609, 476, 273, 112);
				txtErrors.setEditable(false);
			}
			{
				lblFouten = new JLabel();
				getContentPane().add(lblFouten);
				lblFouten.setText("Fouten:");
				lblFouten.setBounds(609, 455, 63, 21);
			}
			{
				jsep1 = new JSeparator();
				getContentPane().add(jsep1);
				jsep1.setBounds(609, 315, 273, 7);
			}
			{
				jsep2 = new JSeparator();
				getContentPane().add(jsep2);
				jsep2.setBounds(609, 448, 273, 28);
			}
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			pack();
			setSize(895 + this.getInsets().left, 
					600 + this.getInsets().top);
			setTitle("JDammen");
			this.setResizable(false);
			setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void messageReceived(String message)
	{
		txtReceived.append("<" + zijnNick + "> " + message + "\n");
	}
	
	public void addError(String error)
	{
		txtErrors.append(sdf.format(new Date()) + error + "\n");
	}
	
	private void messageActionPerformed(ActionEvent evt)
	{
		txtReceived.append("<" + mijnNick + "> " + txtMessage.getText() + "\n");
		gc.sendText(txtMessage.getText());
		txtMessage.setText("");
	}

}
